package net.alpenviewer.app;
import java.awt.image.RenderedImage;
import java.io.File;

import javax.imageio.ImageIO;

import net.alpenviewer.map.ColorEncoder;
import net.alpenviewer.map.CommonElevationColorEncoder;
import net.alpenviewer.map.MapBuilder;
import net.alpenviewer.map.SRTM;

public class ReliefElevationMaps {

	private static final String IMAGE_TYPE = "png";
	private static final File preprocessedDir = new File("srtm");
	private static final File elevationDir    = new File("relief/elevation");

	static {
		if (!preprocessedDir.exists()) {
			preprocessedDir.mkdir();
		} else if (!preprocessedDir.isDirectory()) {
			throw new IllegalArgumentException(preprocessedDir.getName() + " is not a directory");
		}
		if (!elevationDir.exists()) {
			elevationDir.mkdirs();
		} else if (!elevationDir.isDirectory()) {
			throw new IllegalArgumentException(elevationDir.getName() + " is not a directory");
		}
	}

	public static void main(String[] args) throws Exception {

		long t = System.currentTimeMillis();

		final ColorEncoder elevationColor = new CommonElevationColorEncoder();
		
		
		{ // legends
			final int w = 50; final int h = 120;
			
			final RenderedImage elevationLegendM = elevationColor.getVerticalLegend(w, h, true);
			final File out1 = new File(elevationDir.getPath() + File.separator + "hgtLegendM." + IMAGE_TYPE);
			ImageIO.write(elevationLegendM, IMAGE_TYPE, out1);
			
			final RenderedImage elevationLegendF = elevationColor.getVerticalLegend(w, h, false);
			final File out2 = new File(elevationDir.getPath() + File.separator + "hgtLegendF." + IMAGE_TYPE);
			ImageIO.write(elevationLegendF, IMAGE_TYPE, out2);
		}
	
		{
			final File[] files = preprocessedDir.listFiles();
			for (File file : files) {

				if (file.isFile() && file.getName().toLowerCase().endsWith("." + SRTM.SUPPORTED_FILE_EXT)) {
										
					final SRTM srtm = new SRTM(file);
					System.out.println(srtm.toString());
					
					final RenderedImage img = MapBuilder.createElevationImage(srtm, elevationColor);

					final String elevationFilename = srtm.toString() + ".wgs84." + IMAGE_TYPE;
					final File out = new File(elevationDir.getPath() + File.separator + elevationFilename);
					ImageIO.write(img, IMAGE_TYPE, out);					
					
				}

			}	
		}
		
		t = System.currentTimeMillis() - t;		
		System.out.println("processing elapsed " + t + " millis.");
	}
	
	

}
